﻿Imports System.Windows.Markup
Imports Microsoft.Phone.Shell
Imports LSW.WP8.Extension
Imports Microsoft.Phone.Controls

Namespace Controls
    <ContentProperty("Buttons")>
    Public Class BindableApplicationBar
        Inherits ItemsControl
        Implements IApplicationBar

        ReadOnly _applicationBar As ApplicationBar

        Public Sub New()
            _applicationBar = New ApplicationBar
        End Sub

        Protected Overrides Sub OnItemsChanged(e As Collections.Specialized.NotifyCollectionChangedEventArgs)
            MyBase.OnItemsChanged(e)
            _applicationBar.Buttons.Clear()
            _applicationBar.MenuItems.Clear()
            For Each btn As BindableApplicationBarIconButton In Items.Where(Function(c) TypeOf c Is BindableApplicationBarIconButton)
                _applicationBar.Buttons.Add(btn.Button)
            Next
            For Each btn As BindableApplicationBarMenuItem In Items.Where(Function(c) TypeOf c Is BindableApplicationBarMenuItem)
                _applicationBar.MenuItems.Add(btn.MenuItem)
            Next
        End Sub

        Public Shared ReadOnly IsVisibleProperty As DependencyProperty = DependencyProperty.RegisterAttached("IsVisible", GetType(Boolean), GetType(BindableApplicationBar), New PropertyMetadata(True, AddressOf OnVisibleChanged))

        Public Property BackgroundColor As Color Implements IApplicationBar.BackgroundColor
            Get
                Return _applicationBar.BackgroundColor
            End Get
            Set(value As Color)
                _applicationBar.BackgroundColor = value
            End Set
        End Property

        Public ReadOnly Property Buttons As Collections.IList Implements IApplicationBar.Buttons
            Get
                Return Items
            End Get
        End Property

        Public ReadOnly Property DefaultSize As Double Implements IApplicationBar.DefaultSize
            Get
                Return _applicationBar.DefaultSize
            End Get
        End Property

        Public Property ForegroundColor As Color Implements IApplicationBar.ForegroundColor
            Get
                Return _applicationBar.ForegroundColor
            End Get
            Set(value As Color)
                _applicationBar.ForegroundColor = value
            End Set
        End Property

        Public Property IsMenuEnabled As Boolean Implements IApplicationBar.IsMenuEnabled
            Get
                Return CBool(GetValue(IsMenuEnabledProperty))
            End Get
            Set(value As Boolean)
                SetValue(IsMenuEnabledProperty, value)
            End Set
        End Property

        Public Property IsVisible As Boolean Implements IApplicationBar.IsVisible
            Get
                Return CBool(GetValue(IsVisibleProperty))
            End Get
            Set(value As Boolean)
                SetValue(IsVisibleProperty, value)
            End Set
        End Property

        Public Property BarOpacity As Double Implements IApplicationBar.Opacity
            Get
                Return _applicationBar.Opacity
            End Get
            Set(value As Double)
                _applicationBar.Opacity = value
            End Set
        End Property

        Public ReadOnly Property MenuItems As Collections.IList Implements IApplicationBar.MenuItems
            Get
                Return Items
            End Get
        End Property

        Public ReadOnly Property MiniSize As Double Implements IApplicationBar.MiniSize
            Get
                Return _applicationBar.MiniSize
            End Get
        End Property

        Public Property Mode As ApplicationBarMode Implements IApplicationBar.Mode

        Public Event StateChanged(sender As Object, e As ApplicationBarStateChangedEventArgs) Implements IApplicationBar.StateChanged

        Private Sub BindableApplicationBar_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
            Dim page = DirectCast(GetVisualAncestors.FirstOrDefault(Function(c) TypeOf c Is PhoneApplicationPage), PhoneApplicationPage)
            If page IsNot Nothing Then page.ApplicationBar = _applicationBar
        End Sub

        Private Shared Sub OnVisibleChanged(d As DependencyObject, e As DependencyPropertyChangedEventArgs)
            If e.NewValue <> e.OldValue Then
                CType(d, BindableApplicationBar)._applicationBar.IsVisible = CBool(e.NewValue)
            End If
        End Sub

        Public Shared ReadOnly IsMenuEnabledProperty As DependencyProperty = DependencyProperty.RegisterAttached("IsMenuEnabled", GetType(Boolean), GetType(BindableApplicationBar), New PropertyMetadata(True, AddressOf OnEnabledChanged))

        Private Shared Sub OnEnabledChanged(d As DependencyObject, e As DependencyPropertyChangedEventArgs)
            If e.NewValue <> e.OldValue Then
                CType(d, BindableApplicationBar)._applicationBar.IsMenuEnabled = CBool(e.NewValue)
            End If
        End Sub

    End Class
End Namespace